package cn.dapeng.ticket;

/**
 * @BelongProject study
 * @BelongPackage cn.dapeng.thread
 * @Description 模拟10个线程买100张票
 * @ClassName SaleTicketExample
 * @Author xupenghong
 * @Date 2024/8/28 10:21
 * @Version 1.0.0
 */
public class SaleTicketExample {

    private static int ticket = 100;

    public synchronized static void sale() {
        while (ticket > 0) {
            ticket--;
            System.out.println(Thread.currentThread().getName() + " sale ticket " + ticket);
        }
    }

    public static void main(String[] args) {
        int threadCount = 10;
        Thread[] threads = new Thread[threadCount];
        for (int i = 0; i < threadCount; i++) {
            threads[i] = new Thread(SaleTicketExample::sale);
            threads[i].start();
        }
    }
}
